# -*- coding: utf-8 -*-
from flask import jsonify, request, render_template, redirect, session, url_for
from framework.model import Pusher as PusherModel, PusherWebhook as PusherWebhookModel
from framework.app import db
from framework import setting
from framework.views import view_blue
import hashlib
import time

# 获取配置dingtalk列表
@view_blue.route('/pusher/list', endpoint="pusher-list")
def getList():
    result = PusherModel.query.all()
    list_array = []
    for item in result:
        list_array.append({
            "id": item.id,
            "pusher_name": item.pusher_name,
            "pusher_url": "{}/api/pusher/send-msg/{}".format(setting.BASE_DOMAIN, item.pusher_secret),
            "pusher_note": item.pusher_note,
            "create_time": item.create_time
        })
    return render_template('pusher-list.html', webList = list_array)

@view_blue.route('/pusher/add', methods=['POST'], endpoint="pusher-add")
def addPusher():
    try:
        pusher = PusherModel()
        pusher.pusher_name = request.form.get("name")
        pusher.pusher_note = request.form.get("note")
        pusher.pusher_secret = hashlib.md5(str(time.time()).encode("utf-8")).hexdigest()
        pusher.user_id = session['user_id']
        db.session.add(pusher)
        db.session.commit()
        return redirect(url_for("view.pusher-list"))
    except Exception as e:
        return jsonify({"code":400, "msg":repr(e)})

@view_blue.route('/pusher-webhook/add', methods=['POST'], endpoint="pusher-webhook-add")
def addPusherWebhook():
    try:
        pusher_id = request.form.get("pusher_id")
        pusher_type = request.form.get("pusher_type")
        pusher_webhook = PusherWebhookModel.query.filter(PusherWebhookModel.pusher_id == pusher_id, PusherWebhookModel.pusher_type == pusher_type).first()
        if pusher_webhook:
            raise ValueError(pusher_type + "类型的webhook已经存在")
        pusher = PusherWebhookModel()
        pusher.pusher_id = pusher_id
        pusher.address_name = request.form.get("address_name")
        pusher.webhook = request.form.get("webhook")
        pusher.secret = request.form.get("secret")
        pusher.pusher_type = pusher_type
        pusher.user_id = session['user_id']
        db.session.add(pusher)
        db.session.commit()
        return redirect(url_for("view.pusher-list"))
    except Exception as e:
        return jsonify({"code":400, "msg":repr(e)})

@view_blue.route('/pusher-webhook/list/<id>', methods=['GET'], endpoint="pusher-webhook-list")
def getPusherWebhook(id):
    try:
        pusher_webhook_list = PusherWebhookModel.query.filter(PusherWebhookModel.pusher_id==id).all()
        list_array = []
        for item in pusher_webhook_list:
            list_array.append({
                "address_name": item.address_name,
                "webhook": item.webhook,
                "secret": item.secret,
                "pusher_type": item.pusher_type
            })
        return jsonify({"code":200, "msg":"success", "data": list_array})
    except Exception as e:
        return jsonify({"code":400, "msg":repr(e)})


@view_blue.route('/pusher/del/<id>', endpoint="pusher-del")
def delPusher(id):
    try:
        PusherModel.query.filter(PusherModel.id == id).delete()
        db.session.commit()
        return redirect('/view/pusher/list')
    except Exception as e:
        return jsonify({"code":400, "msg":repr(e)})


